import Vue from 'vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router'
import routes from './router'
import axios from "axios";
import VueAxios from 'vue-axios'
import App from './App.vue'
import VueCookies from 'vue-cookies'

axios.defaults.baseURL='/api'

Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
Vue.use(VueCookies);
Vue.config.productionTip = false

const router = new VueRouter({
  mode: 'history',
  routes
})

// this.$dateTime(time)调用函数
Vue.prototype.$dateTime = time => {
  let newDate = new Date(time);
  let dateCollection = { y: newDate.getFullYear(), M: newDate.getMonth() + 1, d: newDate.getDate(), h: newDate.getHours(), m: newDate.getMinutes(), s: newDate.getSeconds() };
  for(let i in dateCollection) {
    if (dateCollection[i] < 10) dateCollection[i] = `0${dateCollection[i]}`;
  }
  let { y, M, d, h, m, s } = dateCollection;
  return `${y}-${M}-${d} ${h}:${m}:${s}`;
};


// 定义全局方法用于操作localStorage
Vue.prototype.$blockInfo = {
  setBlockInfo(traceNumber, blockData) {
    // 获取当前所有数据
    const currentData = JSON.parse(localStorage.getItem('blockInfo') || '{}');
    // 更新数据
    currentData[traceNumber] = blockData;
    // 保存回localStorage
    localStorage.setItem('blockInfo', JSON.stringify(currentData));
  },
  getBlockInfo(traceNumber) {
    // 获取当前所有数据
    const currentData = JSON.parse(localStorage.getItem('blockInfo') || '{}');
    // 返回指定traceNumber的数据
    return currentData[traceNumber];
  },
  clearBlockInfo(){
    localStorage.clear();
  }
};

new Vue({
  router: router,
  render: h => h(App),
}).$mount('#app')
